package com.huirui.server.controller.mini.program;

import com.huirui.server.context.UserContext;
import com.huirui.server.pojo.entity.BrowseHistory;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.BrowseHistoryAnotherVo;
import com.huirui.server.pojo.vo.BrowseHistoryVo;
import com.huirui.server.service.BrowseHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/api/history")
@Slf4j
@Validated
@Tag(name = "浏览历史管理", description = "微信小程序用户浏览历史查询相关")
public class BrowseHistoryController {

    @Resource
    private BrowseHistoryService browseHistoryService;

    @GetMapping
    @Operation(summary = "获取用户所有的浏览历史")
    public Result<List<BrowseHistoryVo>> listUserBrowseHistory() {
        List<BrowseHistoryVo> res = browseHistoryService.listUserBrowseHistory();
        return Result.success(res);
    }

    @GetMapping("/count")
    @Operation(summary = "查询浏览历史的数量")
    public Result<Long> getCountOfUserBrowseHistory() {
        Long count = browseHistoryService.lambdaQuery().eq(BrowseHistory::getUserId, UserContext.getId()).count();
        return Result.success(count);
    }

    @DeleteMapping("/all")
    @Operation(summary = "删除所有的浏览历史")
    public Result<Void> deleteAllBrowseHistory() {
        browseHistoryService.lambdaUpdate().eq(BrowseHistory::getUserId, UserContext.getId()).remove();
        return Result.success();
    }

    @DeleteMapping
    @Operation(summary = "根据商品id列表删除商品")
    public Result<Void> deleteBrowseHistoryByIds(@RequestParam("ids") List<Long> ids) {
        browseHistoryService
                .lambdaUpdate()
                .eq(BrowseHistory::getUserId, UserContext.getId())
                .in(BrowseHistory::getProductId, ids)
                .remove();
        return Result.success();
    }

}
